package com.ashokvarma.bottomnavigation;

import ohos.agp.render.Canvas;
import ohos.agp.render.Texture;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.AlphaType;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.util.Optional;

public class BottomNavigationItem {
    private PixelMap orgPixelMap;
    private PixelMap orgInActivePixelMap;

    private PixelMap activePixelMap;
    private PixelMap inActivePixelMap;
    private PixelMap backgroundPixelMap;

    private int iconSize;

    private int mIconResource;

    private int mInactiveIconResource;
    private boolean inActiveIconAvailable = false;

    private int mTitleResource;
    private String mTitle;

    private int mActiveColor;

    private int mInActiveColor;
    private int mBackgrounColor;

    private BadgeItem badgeItem;

    private BottomNavigationBar bottomNavigationBar;

    public BottomNavigationItem(int mIconResource, String mTitle, Context mContext) {
        this.mIconResource = mIconResource;
        this.mTitle = mTitle;
        Optional<PixelMap> optional = ResUtil.getPixelMap(mContext, mIconResource);
        orgPixelMap = optional.isPresent() ? optional.get() : null;
    }

    public BottomNavigationItem(PixelMap pixelMap, String mTitle) {
        this.orgPixelMap = pixelMap;
        this.mTitle = mTitle;
    }

    public BottomNavigationItem setInactiveIconResource(int mInactiveIconResource, Context mContext) {
        this.mInactiveIconResource = mInactiveIconResource;
        Optional<PixelMap> optional = ResUtil.getPixelMap(mContext, mInactiveIconResource);
        orgInActivePixelMap = optional.isPresent() ? optional.get() : null;
        inActiveIconAvailable = true;
        return this;
    }

    public BottomNavigationItem setActiveColor(int color) {
        this.mActiveColor = color;
        return this;
    }

    public BottomNavigationItem setInActiveColor(int color) {
        this.mInActiveColor = color;
        return this;
    }

    public String getTitle() {
        return this.mTitle;
    }

    PixelMap getInactiveIcon() {
        return orgPixelMap;
    }

    boolean isInActiveIconAvailable() {
        return inActiveIconAvailable;
    }

    int getActiveColor() {
        return mActiveColor;
    }

    int getInActiveColor() {
        return mInActiveColor;
    }

    int getBackgroundColor() {
        return mBackgrounColor;
    }

    public BottomNavigationItem setBadgeItem(BadgeItem badgeItem) {
        badgeItem.bottomNavigationBar = this.bottomNavigationBar;
        this.badgeItem = badgeItem;
        return this;
    }

    public BadgeItem getBadgeItem() {
        return badgeItem;
    }

    void setupPixelMap(BottomNavigationBar bottomNavigationBar, int iconSize) {
        this.bottomNavigationBar = bottomNavigationBar;
        if (badgeItem != null) {
            badgeItem.bottomNavigationBar = bottomNavigationBar;
        }
        this.iconSize = iconSize;
        this.mBackgrounColor = bottomNavigationBar.getBackgroundColor();
        if (orgPixelMap != null) {
            generateIconBitmaps(bottomNavigationBar);
        }
    }

    PixelMap getActivePixelMap() {
        return activePixelMap;
    }

    PixelMap getInActivePixelMap() {
        return inActivePixelMap;
    }

    PixelMap getBackgroundPixelMap() {
        return backgroundPixelMap;
    }

    private PixelMap copy(PixelMap source, int width, int height) {
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
        initializationOptions.alphaType = AlphaType.PREMUL;
        initializationOptions.size = new Size(width, height);
        return PixelMap.create(source, initializationOptions);
    }

    private PixelMap scaleIcon(PixelMap origin) {
        int width = origin.getImageInfo().size.width;
        int height = origin.getImageInfo().size.height;
        int size = Math.max(width, height);
        if (size == iconSize) {
            return origin;
        } else if (size > iconSize) {
            int scaledWidth;
            int scaledHeight;
            if (width > iconSize) {
                scaledWidth = iconSize;
                scaledHeight = (int) (iconSize * ((float) height / width));
            } else {
                scaledHeight = iconSize;
                scaledWidth = (int) (iconSize * ((float) width / height));
            }
            return copy(origin, scaledWidth, scaledHeight);
        } else {
            return origin;
        }
    }

    private void generateIconBitmaps(BottomNavigationBar bottomNavigationBar) {
        if (orgPixelMap == null) {
            return;
        }
        orgPixelMap = scaleIcon(orgPixelMap);
        if (orgInActivePixelMap != null) {
            orgInActivePixelMap = scaleIcon(orgInActivePixelMap);
        }
        PixelMap origin = orgPixelMap;
        activePixelMap = copy(origin, origin.getImageInfo().size.width, origin.getImageInfo().size.height);
        Canvas canvas = new Canvas(new Texture(activePixelMap));
        canvas.drawColor(
                mActiveColor != 0 ? mActiveColor : bottomNavigationBar.getActiveColor(), Canvas.PorterDuffMode.SRC_IN);
        if (inActiveIconAvailable && orgInActivePixelMap != null) {
            inActivePixelMap =
                    copy(
                            orgInActivePixelMap,
                            orgInActivePixelMap.getImageInfo().size.width,
                            orgInActivePixelMap.getImageInfo().size.height);
        } else {
            inActivePixelMap = copy(origin, origin.getImageInfo().size.width, origin.getImageInfo().size.height);
            canvas = new Canvas(new Texture(inActivePixelMap));
            canvas.drawColor(
                    mInActiveColor != 0 ? mInActiveColor : bottomNavigationBar.getInActiveColor(),
                    Canvas.PorterDuffMode.SRC_IN);
        }
        backgroundPixelMap = copy(origin, origin.getImageInfo().size.width, origin.getImageInfo().size.height);
        canvas = new Canvas(new Texture(backgroundPixelMap));
        canvas.drawColor(mBackgrounColor, Canvas.PorterDuffMode.SRC_IN);
    }
}
